﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using WPF_DbCenter.CommonMethod;
using WPF_DbCenter.DbEnginer;
using System.Data;
using System.Diagnostics;
using System.Data.SqlServerCe;
using XLCS.Common;
using CustomControl.DynaProgressBar;

namespace WPF_DbCenter.AdditionUI
{
    /// <summary>
    /// Interaction logic for SqlServer2Sdf.xaml
    /// </summary>
    public partial class SqlServer2Sdf : BaseUI.BaseFadeDialog
    {
        public SqlServer2Sdf()
        {
            InitializeComponent();
            Title = "Sql server sync to Sql CE";
            WindowStartupLocation = WindowStartupLocation.CenterOwner;

#if DEBUG
            button1.Visibility = Visibility.Visible;
#else
            button1.Visibility = Visibility.Hidden;
#endif

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.txtServername.Text = "localhost";
            this.txtDbName.Text = "test";
            this.txtUsername.Text = "sa";
            passwordBox1.Password = "test";
        }

        private void butSync_Click(object sender, RoutedEventArgs e)
        {
            string server = txtServername.Text;
            string database = this.txtDbName.Text;
            string username = this.txtUsername.Text;
            string pwd = passwordBox1.Password;

            if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database)
                || string.IsNullOrEmpty(username))
            {
                MessageBox.Show("Please input each element");
                return;
            }
            string sdfFile = txtTargetFile.Text;

            if (string.IsNullOrEmpty(sdfFile))
            {
                MessageBox.Show("Please input target file path");
                return;
            }

            PageSwitchProgressBar dp = PageSwitchProgressBar.X_BeginLoadingDialog();

            try
            {
                bool isNeedCopyData = false;
                if ((checkBox1.IsChecked.HasValue) && (checkBox1.IsChecked.Value == true))
                {
                    isNeedCopyData = true;
                }

                List<string> proecssTableList = new List<string>();
                foreach (string item in listBox1.SelectedItems)
                {
                    proecssTableList.Add(item);
                }

                if (CommonUtil.SyncDataFromSqlServerToSqlCE(server, database, username, pwd, sdfFile, isNeedCopyData, (bool)chkIsTrustedConn.IsChecked, proecssTableList))
                {
                    dp.X_EndLoadingDialog();
                    MessageBox.Show(String.Format("Sync data from \r\n{0} \r\nto \r\n{1} \r\nSuccessful", database, sdfFile));
                }
            }
            catch
            {

            }
            finally
            {
                if (!dp.IsDisposed)
                {
                    dp.X_EndLoadingDialog();
                }

            }
        }

        private void butGetAllTablesList_Click(object sender, RoutedEventArgs e)
        {
            string server = txtServername.Text;
            string database = this.txtDbName.Text;
            string username = this.txtUsername.Text;
            string pwd = passwordBox1.Password;

            if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(database)
                || string.IsNullOrEmpty(username))
            {
                MessageBox.Show("Please input each element");
                return;
            }
            
            CoreEA.ICoreEAHander core = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer).X_Handler;
            CoreEA.LoginDbInfo info = new CoreEA.LoginDbInfo(server, database, "", username, pwd);
            info.IsTrustedConn = (bool)chkIsTrustedConn.IsChecked;
            try
            {
                core.Open(info);
                this.listBox1.Items.Clear();
                foreach (string item in core.GetTableListInDatabase(database))
                {
                    listBox1.Items.Add(item);
                }
            }
            catch (Exception ee)
            {
                XLCS.Common.ProcessException.DisplayErrors(ee);
            }

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog sf = new OpenFileDialog();
            sf.Filter = AppEntry.SQLCE_FILE_FILTER;
            if (sf.ShowDialog() == true)
            {
                this.txtTargetFile.Text = sf.FileName;
            }
        }

    }
}
